<?php

declare(strict_types=1);

namespace App\Controller\Kpi;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Annotation\Scene;
use Qbhy\HyperfAuth\AuthManager;

use App\Service\SysUser as SysUserService;
use App\Service\AuthMenu as AuthMenuService;
use App\Request\SysUserRequest;

#[AutoController]
class SysUserController extends AbstractController
{
    #[Inject]
    private AuthManager $auth;

    // 重置商户密码
    public function resetpwd()
    {
        $user_service = new SysUserService();
        $info = $user_service->getInfo($this->auth->guard('jwt')->id());
        if (!$info) {
            return $this->returnError('用户不存在', 500);
        }

        $orgin_password = $this->request->input('orgin_password', '');
        $password = $this->request->input('password', '');

        if (!$orgin_password || !$password) {
            return $this->returnError('密码错误', 500);
        }

        // 验证密码是否正确
        if (!password_verify($orgin_password, $info['password'])) {
            return $this->returnError('原始密码错误', 500);
        }

        if ($orgin_password == $password) {
            return $this->returnError('新密码不能与原密码一致', 500);
        }

        // 更新密码
        $result = $user_service->updateUserInfoById($info['uid'], ['password' => password_hash($password, PASSWORD_DEFAULT)]);
        return $this->returnSuccess($result, '密码修改成功');
    }

    // 管理员列表
    public function user_list()
    {
        $map = [];
        $username = $this->request->input('username', '');
        if ($username) {
            $map[] = ['username', '=', $username];
        }

        $result = (new SysUserService())->userList($map);
        return $this->returnSuccess($result, '成功');
    }

    // 管理员列表
    public function delete_user()
    {
        $uid = $this->request->input('uid', 0);
        if (!$uid)
            return $this->returnError('请求参数错误');
        $result = (new SysUserService())->deleteUser($uid);
        return $this->returnSuccess($result, '删除成功');
    }

    // 添加管理员
    #[Scene(scene: 'add', argument: 'request')]
    public function add_user(SysUserRequest $request)
    {
        // $params = $request->validated();
        $params = $this->request->all();
        $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
        $data = (new SysUserService())->addSysUser($params);
        return $this->returnSuccess($data, '添加成功');
    }

    // 编辑管理员
    #[Scene(scene: 'edit', argument: 'request')]
    public function edit_user(SysUserRequest $request)
    {
        $params = $this->request->all();

        $info = (new SysUserService())->getInfo($params['uid']);
        if (!$info) {
            return $this->returnError('账号信息错误');
        }
        if ($info['is_admin'] == 1) {
            return $this->returnError('超级管理员禁止编辑');
        }
        if ($params['password']) {
            $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
        } else {
            unset($params['password']);
        }
        $data = (new SysUserService())->editSysUser($params['uid'], $params);
        return $this->returnSuccess($data, '编辑成功');
    }

    // 获取用户菜单权限
    public function get_menu_auth()
    {
        $user_service = new SysUserService();
        $info = $user_service->getInfo($this->auth->guard('jwt')->id());
        if (!$info) {
            return $this->returnError('出错啦，请重新登录', 500);
        }

        $is_admin = $info['is_admin'] ? true : false;
        $menu_list = (new AuthMenuService())->getAuthMenuByRoleID($info['role_id'],$is_admin);
        return $this->returnSuccess($menu_list, '请求成功');
    }
}
